package com.example.homework_6;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ImageView back_down, move_forward;
    EditText website;
    Button search, history;
    WebView check_website;
    ListView history_list;

    List<String> history_data = new ArrayList<>();
    boolean is_click_history = false;
    ArrayAdapter<String> historyAdapter;
    SharedPreferences sharedPreferences;
    private static final String PREFS_NAME = "User_Data";
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_main);

        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        load_data();

        back_down = findViewById(R.id.back_down);
        move_forward = findViewById(R.id.move_forward);
        website = findViewById(R.id.website);
        search = findViewById(R.id.search);
        history = findViewById(R.id.history);
        check_website = findViewById(R.id.check_website);
        history_list = findViewById(R.id.history_list);

        check_website.getSettings().setJavaScriptEnabled(true);

        check_website.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);
            }
        });

        check_website.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                website.setText(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_click_history) {
                    check_website.setVisibility(View.VISIBLE);
                    history_list.setVisibility(View.GONE);
                    is_click_history = false;
                }
                else {
                    history_list.setVisibility(View.VISIBLE);
                    check_website.setVisibility(View.GONE);
                    is_click_history = true;
                    historyAdapter = new ArrayAdapter<>(MainActivity.this,
                            android.R.layout.simple_list_item_1, history_data);
                    history_list.setAdapter(historyAdapter);

                    history_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            check_website.loadUrl(history_data.get(i));
                            check_website.setVisibility(View.VISIBLE);
                            history_list.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (android.webkit.URLUtil.isNetworkUrl(website.getText().toString().trim())) {
                    check_website.loadUrl(website.getText().toString());
                    check_website.setVisibility(View.VISIBLE);
                    history_list.setVisibility(View.GONE);
                    is_click_history = false;
                    if (!history_data.contains(website.getText().toString())) {
                        history_data.add(website.getText().toString());
                        sharedPreferences.edit().putString(i + "", website.getText().toString()).apply();
                        i += 1;
                    }
                }
                else {
                    Toast.makeText(MainActivity.this, "网址格式不正确，请重新输入", Toast.LENGTH_SHORT).show();
                    website.requestFocus();
                }
            }
        });

        back_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (check_website.canGoBack()) {
                    check_website.goBack();
                }
                else {
                    Toast.makeText(MainActivity.this, "无法后退", Toast.LENGTH_SHORT).show();
                }
            }
        });

        move_forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (check_website.canGoForward()) {
                    check_website.goForward();
                }
                else {
                    Toast.makeText(MainActivity.this, "无法前进", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void load_data() {
        while (!sharedPreferences.getString(i + "", "").isEmpty()) {
            history_data.add(sharedPreferences.getString(i + "", ""));
            i += 1;
        }
    }
}